﻿#include "wdownwidget.h"
#include "ui_wdownwidget.h"
#include<QFile>
#include<QTextStream>
#include<QFileDialog>
#include<QMessageBox>
#include<QPushButton>
#include <QTextCodec>
#include<QDebug>
#include<QDir>
#include<QScreen>

wdownWidget::wdownWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::wdownWidget)
{
    qDebug()<< QDir::currentPath();

    globalMark=0;
    ui->setupUi(this);
    connect(ui->pushButton_image,SIGNAL(clicked()),this,SLOT(openbtn()));
    connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(pushbutton_clicked()));
}
wdownWidget::~wdownWidget()
{
    delete ui;
}

void wdownWidget::on_back_btn_clicked()
{
    //this->save();
    this->close();
}
void wdownWidget::pushbutton_clicked()
{
    if(ui->textEdit->toPlainText() == "")
            {
                QMessageBox::warning(this,tr("警告"),tr("内容不能为空!"),QMessageBox::Ok);
            }
    else
            {
                QFileDialog fileDialog;
                QString str = fileDialog.getSaveFileName(this,tr("选择路径保存"),"D:/Qt/Qt5.3/Tools/QtCreator/bin/build-SE_tour-Desktop_Qt_5_3_MinGW_32bit-Debug",tr("Text File(*.txt)"));
                if(str == "")
                {
                    return;
                }
                QFile filename(str);
                if(!filename.open(QIODevice::WriteOnly | QIODevice::Text))
                {
                    QMessageBox::warning(this,tr("错误"),tr("打开文件失败"),QMessageBox::Ok);
                    return;
                }
                else
                {
                    QTextStream textStream(&filename);
                    QString str = ui->textEdit->toPlainText();
                    textStream<<str;
                }
                QMessageBox::information(this,"保存文件","保存文件成功",QMessageBox::Ok);
                filename.close();
                //编码格式支持中文
                QTextCodec * textcodec=QTextCodec::codecForName("gbk");
                //追加地点
                filename.open(QIODevice::Append);
                QString str_location;
                str_location=ui->lineEdit_location->text();
                QByteArray byte_location = str_location.toLocal8Bit();
                 QString location="地点:";
                QByteArray locationbyte=location.toLocal8Bit();
                filename.write("\n");
                filename.write(locationbyte);
                filename.write(byte_location);
                filename.close();
                //追加天气
                filename.open(QIODevice::Append);
                QString str_weather;
                str_weather=ui->lineEdit_weather->text();
                QByteArray byte_weather = str_weather.toLocal8Bit();
                 QString weather="天气:";
                QByteArray weatherbyte=weather.toLocal8Bit();
                filename.write("\n");
                filename.write(weatherbyte);
                filename.write(byte_weather);
                filename.close();
                //追加心情
                filename.open(QIODevice::Append);
                QString str_mood;
                str_mood=ui->lineEdit_mood->text();
                QByteArray byte_mood= str_mood.toLocal8Bit();
                QString mood="心情:";
                QByteArray moodbyte=mood.toLocal8Bit();
                filename.write("\n");
                filename.write(moodbyte);
                filename.write(byte_mood);
                filename.close();


    }
}
void wdownWidget::openbtn()
{
    ui->label_image->clear();//先清除label内容
        //打开图片操作
        QString fileName = QFileDialog::getOpenFileName(this, tr("open image file"),"D:/Qt/Qt5.3/Tools/QtCreator/bin/build-SE_tour-Desktop_Qt_5_3_MinGW_32bit-Debug", tr("Image files(*.bmp *.jpg *.pbm *.pgm *.png *.ppm *.xbm *.xpm);;All files (*.*)"));

      /*  QString filepath = "C:\\Users\\zhang zi long\\Pictures\\image";  //图片的绝对路径
        QString curPath = QDir::currentPath();
        QString relPath = filepath.mid(curPath.length()+1);*/
        if(fileName.isEmpty())
        {
            QMessageBox mesg;
            mesg.warning(this,"警告","打开图片失败!");
            return;
        }
        else
        {
            QImage img(fileName);
            img=img.scaled(ui->label_image->width(),ui->label_image->height(),Qt::KeepAspectRatio);//Qt::KeepAspectRatio自适应大小，不变形
            ui->label_image->setPixmap(QPixmap::fromImage(img));
            if(img.isNull())
            {
                QMessageBox::information(this,"信息","打开文件失败");
            }
            else
            {
                globalMark=1;//存储标记，用于判断是否有文件
                globalImg=img;
            }
        }

}
void wdownWidget::on_pushButton_2_clicked()
{
    QString filename1 = QFileDialog::getSaveFileName(this,tr("Save Image"),"E:\\",tr("Images (*.png *.bmp *.jpg)")); //选择路径
    QScreen *screen = QGuiApplication::primaryScreen();
    screen->grabWindow(ui->label_image->winId()).save(filename1);

}

